#ifndef __STN_H__
#define __STN_H__

#define STN_ERROR_TRIES 5
#define STN_NONE_RETURN -200

#define PROTOCOL_TYPE_J1850	0
#define PROTOCOL_TYPE_KWP		1
#define PROTOCOL_TYPE_CAN		2

#define KWP_HEAD_LEN 		9
#define KWP_MSG_LEN 			26

#define BYTE_LEN				2
#define CMD_RTN_LEN			5

#define CAN_ADDR_LEN			3					// CAN Address Length 7E8
#define CAN_HEAD_LEN 		7					// '7E8 XX 4X XX' Only One message return
														// '7E8 10 XX 4X ..." Multi lins return
#define CAN_COUNT_LEN		3					// Count Byte length of return message (with space)
#define CAN_MSG_LEN			28					// Count of each return line (with space)
#define CAN_RES_MSG_LEN		2					//Without count the space
#define CAN_PID_MSG_LEN		15
	
#define CAN_ONE_MSG_LEN		21					// Content Length of One Line Message
#define CAN_10_MSG_LEN		18					// Content Length of First Mulit Line
#define CAN_2X_MSG_LEN		21					// Content Length of Other Mulit Lines

#define CAN_VIN_MSG_LEN		21
#define CAN_VIN_LINES		3					// '09 02' command return 3 lines content
#define CAN_CALID_LINES		3					// '09 04' command return 3 lines content

#define CAN_MULTI_MAP_TOTAL 110					// totol return length from multi map
#define CAN_MULTI_MAP_LINES  3					// '01 0B 0F 06 07 0D 0C' command return 3 lines content
#define CAN_MULTI_MAP_PID_NUM 6					// number of registers for map calculation
#define CAN_MULTI_MAP_RETURN_DATA 14			// length of vaild data from multi map CAN command

#define CAN_MULTI_MAF_TOTAL 78
#define CAN_MULTI_MAF_LINES  2					// '01 10 06 07 0D 0C' command return 2 lines content
#define CAN_MULTI_MAF_PID_NUM 5					// number of registers for maf calculation
#define CAN_MULTI_MAF_RETURN_DATA 13			// length of vaild data from multi maf CAN command

#define CAN_MULTI_EFR_LINES  3					// '01 04 05 11 5E 0D 0C' command return 3 lines content
#define CAN_MULTI_EFR_PID_NUM 6					// number of registers for EFR calculation
#define CAN_MULTI_EFR_RETURN_DATA 15			// length of vaild data from multi EFR CAN command

#define CAN_MULTI_DBH_TOTAL	43
#define CAN_MULTI_DBH_LINES 	1					// '01 04 05 11' command return 1 line content
#define CAN_MULTI_DBH_PID_NUM 3					// number of registers for drive behavior calculation
#define CAN_MULTI_DBH_RETURN_DATA 7				// length of vaild data from multi drive behavior CAN command

#define CAN_0100_RES_OFFSET	13					// Command 0100 return message content only
#define CAN_0100_HON_LEN		24					// Command 0100 return message length with can addr
#define CAN_0100_HOFF_LEN		17					// Command 0100 return message length without can addr


#define VIN_LEN					17
#define CALID_LEN					8
#define SN_LEN						8
#define CID_LEN					16
#define CAL_CID_LEN				8


#define CONFIG_LENGTH			0x80
#define CONFIG_VIN_ADDR			0x10
#define CONFIG_SN_ADDR			0x22
#define CONFIG_CID_ADDR			0x2B
#define CONFIG_PID00_ADDR		0x40
#define CONFIG_PID20_ADDR		0x44
#define CONFIG_PID40_ADDR		0x48
#define CONFIG_PID90_ADDR		0x4C
#define CONFIG_GAS_CAL_ADDR	0x50
#define CONFIG_FUEL_CAL_ADDR 	0x51
#define CONFIG_POI_ADDR			0x53
#define CONFIG_DIST_CAL_ADDR	0x54
#define CONFIG_FILE_ID_ADDR	0x60
#define CONFIG_CALID_ADDR		0x70
#define CONFIG_CHECKSUM_ADDR	0x7F

#define FRP_CONSTANT		12
#define FRP_VE				7
#define FRP_ED				24
#define FRP_R				8314
#define FRP_MAP_CONST	1000
#define FRP_MAIR			2897
#define FRP_AFR			1464
#define FRP_FUELP			74
#define FRP_MAF_CONST	10000

extern int processSTNreturn(char * result, int count, int index, int val);

extern int SerialCommand(int fd, char *txbuf, int count, char *rxbuf);
extern void Print_Datas(unsigned char *data, int len, unsigned char *message);
extern void Cal_Vehicle_CID(unsigned char *data);
extern void Add_Config_Checksum(unsigned char *data);
extern int STN_Check_Connect(int fd, unsigned char *rxbuf, unsigned char alpha);
extern unsigned char STN_Check_Protocol(int fd, unsigned char *rxbuf, \
											unsigned char alpha, unsigned char *data);
/* Function For KWP Protocol */
extern int KWP_Check_Connect(int fd, unsigned char *rxbuf, unsigned char alpha);
extern int KWP_Get_PID_Support(int fd, unsigned char *rxbuf, \
											unsigned char alpha, unsigned char * data);
extern int KWP_Get_VIN(int fd, unsigned char *rxbuf, unsigned char alpha, \
											unsigned char * data);
extern int KWP_Get_CALID(int fd, unsigned char *rxbuf, unsigned char alpha, \
											unsigned char * data);
extern int KWP_Get_EngineRPM(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_EngineEFR(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_EngineMAP(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_Fuel_LV(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *vs);
extern int KWP_Get_EngineIAT(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_EngineSTFT(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_EngineLTFT(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_EngineMAF(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_SPEED(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_ENGINE_LOAD(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_COOL_TEMP(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_PAD_POS(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_DIST21(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int KWP_Get_DIST31(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);

/* Function For CAN Protocol */
extern int CAN_Check_Connect(int fd, unsigned char *rxbuf, unsigned char alpha);
extern int CAN_Judge_ECUS(int fd, unsigned char *rxbuf, unsigned char alpha);
extern int CAN_Get_PID_Support(int fd, unsigned char *rxbuf, \
											unsigned char alpha, unsigned char * data);
extern int CAN_Get_VIN(int fd, unsigned char *rxbuf, unsigned char alpha, \
											unsigned char * data);
extern int CAN_Get_CALID(int fd, unsigned char *rxbuf, unsigned char alpha, \
											unsigned char * data);
extern int CAN_Get_EngineRPM(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_EngineEFR(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_EngineMAP(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_Fuel_LV(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *vs);
extern int CAN_Get_EngineIAT(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_EngineSTFT(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_EngineLTFT(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_EngineMAF(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_SPEED(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_ENGINE_LOAD(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_COOL_TEMP(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_PAD_POS(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_DIST21(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Get_DIST31(int fd, unsigned char *rxbuf, unsigned char alpha, \
											struct vehicle_status *);
extern int CAN_Multi_MAP(int fd, unsigned char *rxbuf, unsigned char alpha, \
							struct vehicle_status *vs, struct fuel_rate_para *frp);
extern int CAN_Multi_MAF(int fd, unsigned char *rxbuf, unsigned char alpha, \
								struct vehicle_status *vs, struct fuel_rate_para *frp);
extern int CAN_Multi_EFR(int fd, unsigned char *rxbuf, unsigned char alpha, \
								struct vehicle_status *vs, struct fuel_rate_para *frp);
extern int CAN_GetMultiDriveBehavior(int fd, unsigned char *rxbuf,	\
								unsigned char alpha, 	struct vehicle_status *vs);								
/* Function for Calculation of Fuel Rate */
extern int Cal_FR_From_MAP(struct fuel_rate_para *frp);
extern int Cal_FR_From_MAF(struct fuel_rate_para *frp);
extern int Cal_FR_From_EFR(struct fuel_rate_para *frp);

#endif
